//
//  YHMainViewController.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/19.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        
        let mainTabBar = YHMainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        mainTabBar.addBtn.addTarget(self, action: #selector(YHMainViewController.addBtnClick), forControlEvents: .TouchUpInside)
        
        
    }
    
    @objc private func addBtnClick() {
        
        print("click")
        let composeView = YHComposeView()
        composeView.show(self)
        
//        let window = UIApplication.sharedApplication().keyWindow
//        window?.addSubview(composeView)
    }
    
    private func addChildViewControllers() {
        addChildViewController(YHHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        addChildViewController(YHMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        addChildViewController(YHDiscoveryTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addChildViewController(YHProfileTableViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
        
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String, index: Int) {
        let tabBarItem: YHTabBarItem = YHTabBarItem()
        vc.tabBarItem = tabBarItem
        vc.title = title
        vc.tabBarItem.tag = index
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        let nav = YHBaseNavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        var index = 0
        
        for v in tabBar.subviews{
            
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                if index == item.tag {
                    
                    for subView in v.subviews {
                        if subView.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            
                            UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 4, options: [], animations: { 
                                
                                subView.transform = CGAffineTransformIdentity
                                
                                }, completion: { (_) in
                                    print("OK")
                            })
                        }
                    }
                }
                index += 1
                
            }
        }
    }
    
    
}